package com.talkyun.openx;

import com.talkyun.openx.server.core.Interceptor;
import com.talkyun.openx.server.core.ServiceFactory;
import com.talkyun.openx.server.impl.SimpleServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

@Deprecated
public class RestfulExporter {
    private static final ServiceFactory factory = SimpleServiceFactory.getInstance();
    private static final Logger logger = LoggerFactory.getLogger(RestfulExporter.class);

    protected List<Interceptor> list = new ArrayList<Interceptor>();

    protected Object target;
    protected String mapping;

    public RestfulExporter() {

    }

    public RestfulExporter(Object target, String mapping) {
        this.target = target;
        this.mapping = mapping;
    }

    public void export() {
        this.mapping = this.getLastMapping().toLowerCase();
        if (!mapping.startsWith("/")) {
            this.mapping = "/" + mapping;
        }
        if (list == null || list.isEmpty()) {
            factory.add(mapping, target);
        } else {
            factory.add(mapping, list, target);
        }
    }

    public void setMapping(String mapping) {
        this.mapping = mapping;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public RestfulExporter setInterceptor(List<Interceptor> list) {
        this.list = list;
        return this;
    }

    private String getLastMapping() {
        if (mapping != null) {
            return mapping;
        } else {
            throw new RuntimeException("Not set mapping! " + target);
        }
    }
}